
<template>
  <div class="execution">
    <el-dialog
      :title="'预览数据'"
      :visible="!!previewTable"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :destroy-on-close="true"
      width="80%"
    >   
    <Preview
      v-if="previewTable !== null"
      @close="previewTable = null"
      :target="previewTable"
    />
    </el-dialog>
    <el-dialog
      :title="'查看历史记录'"
      :visible="!!recordTable"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :destroy-on-close="true"
      width="80%"
    >   
    <Record
      v-if="recordTable !== null"
      @close="recordTable = null"
      :target="recordTable"
    />
    </el-dialog>
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @selection-change="selectionChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
      >
        <template
          slot-scope="scope"
          slot="menu"
        >
          <el-button
            type="text"
            icon="el-icon-view"
            size="small"
            @click.stop="handleView(scope.row, scope.index)"
          >预览</el-button>
          <el-button
            type="text"
            icon="el-icon-view"
            size="small"
            @click.stop="handleRecord(scope.row, scope.index)"
          >查看历史记录</el-button>
        </template>
        
      <template slot="menuRight">
          <el-button
            v-if="permissionList.exportDateOpen"
            type="primary"
            size="small"
            @click="exportDateOpen"
            >按日期导出</el-button>
          <el-button
            v-if="permissionList.exportStationOpen"
            type="primary"
            size="small"
            @click="exportStationOpen"
            >按线路导出</el-button>
          <el-button
            type="primary"
            size="small"
	    v-if="false"
            @click="exportAffairsOpen"
            >按财务导出</el-button>
           <!--<el-button
            v-if="permissionList.clickConfirm"
            type="primary"
            size="small"
            @click="confirmClick"
            >一键确认</el-button>
          <el-upload
            class="upload-inline"
            action
            :show-file-list="false"
            accept=".xls, .xlsx"
            :http-request="handleImport"
          >
            <el-button type="primary" size="small">导入</el-button>
          </el-upload> -->
        </template>
      </avue-crud>

      <open-dialog
        :DialogVisible.sync="exportDateVisableDialog"
        title="按日期导出"
        width="40%"
      >
        <el-form label-width="60px" v-if="exportDateVisableDialog">
          <avue-form
            v-model="formCreate"
            :option="exportDateOption"
            style="margin-bottom: 40px"
          ></avue-form>
          <div class="bottom-menu-adjust">
            <el-button type="primary" @click="exportDateClick()">确认</el-button>
            <el-button type="primary" @click="exportDateVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>

      <open-dialog
        :DialogVisible.sync="exportStationVisableDialog"
        title="按线路导出"
        width="40%"
      >
        <el-form label-width="60px" v-if="exportStationVisableDialog">
          <avue-form
            v-model="formCreate"
            :option="exportStationOption"
            style="margin-bottom: 40px"
          ></avue-form>
          <div class="bottom-menu-adjust">
            <el-button type="primary" @click="exportStationClick()">确认</el-button>
            <el-button type="primary" @click="exportStationVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>

      <open-dialog
        :DialogVisible.sync="exportAffairsVisableDialog"
        title="按财务导出"
        width="40%"
      >
        <el-form label-width="60px" v-if="exportAffairsVisableDialog">
          <avue-form
            v-model="formCreate"
            :option="exportAffairsOption"
            style="margin-bottom: 40px"
          ></avue-form>
          <div class="bottom-menu-adjust">
            <el-button type="primary" @click="exportAffairsClick()">确认</el-button>
            <el-button type="primary" @click="exportAffairsVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="confirmVisableDialog"
        title="批量确认"
        width="40%"
      >
        <el-form label-width="60px" v-if="confirmVisableDialog">
          <avue-form
            v-model="formCreate"
            :option="confrmOption"
            style="margin-bottom: 40px"
          ></avue-form>
          <div class="bottom-menu-adjust">
            <el-button type="primary" @click="confirmClick()">确认</el-button>
            <el-button type="primary" @click="confirmVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, 
  preview,
  clickConfirm,
  exportDate,
  exportStation,
  exportAffairs,
  saveBatch } from '@/api/rca/swckstationdailyreportadjustquery'
import { tableOption, prefixInteger, 
  startDate, 
  endDate ,
  exportDateOption,
  exportStationOption,
  exportAffairsOption,
  confrmOption} from '@/const/crud/rca/swckstationdailyreportadjustquery'
import { mapGetters } from 'vuex'
import Preview from "./components/Preview";
import Record from "./components/Record";
import { handleExcelResponse } from "@/util/util";

export default {
  name: 'swckstationdailyreportadjustquery',
  components: {
    Preview,
    Record
  },
  data () {
    return {
      searchForm: {
        tradeDate: [startDate, endDate]
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,

      previewTable: null,
      recordTable: null,
      exportDateVisableDialog: false,
      exportDateOption: exportDateOption,
      exportStationVisableDialog: false,
      exportStationOption: exportStationOption,
      exportAffairsVisableDialog: false,
      exportAffairsOption: exportAffairsOption,
      confirmVisableDialog: false,
      confrmOption: confrmOption,
      formCreate: {},
      selectKeys:[], //存放对像
      selectDateVals:[], //存日期
      selectLineVals:[],//存线路
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList () {
      return {
        exportDateOpen: this.vaildData(
          this.permissions.sw_ck_station_daily_report_exportDateOpen,
          false
        ),
        exportStationOpen: this.vaildData(
          this.permissions.sw_ck_station_daily_report_exportStationOpen,
          false
        ),
        clickConfirm: this.vaildData(
          this.permissions.sw_ck_station_daily_report_clickConfirm,
          false
        ),
      };
    }
  },
  methods: {
    getList (page, params) {
      this.tableLoading = true
      const { tradeDate, lineCode } = this.searchForm
      const [startDate, endDate] = tradeDate || []
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm, {
        startDate,
        endDate,
      })).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    async searchChange (form, done) {
      this.searchForm = { ...form, ...this.extraParam };
      const [startDate, endDate] = this.searchForm.tradeDate || [];
      try {
        await this.getList(this.page, this.searchForm);
      } finally {
        done();
      }
    },

    exportDateOpen(){
      this.exportDateVisableDialog = true;
    },
    async exportDateClick () {//按日期导出
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        const res = await exportDate(this.formCreate);
        if(res.data.size==0){
          this.$message.error('所选时间范围内暂无数据导出，请重新选择！')
        }
        handleExcelResponse(res);
        this.exportDateVisableDialog = false;
      } finally {
        loading.close();
      }
    },

    exportStationOpen(){
      this.exportStationVisableDialog = true;
    },
    async exportStationClick () {//按线路导出
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        const res = await exportStation(this.formCreate);
        if(res.data.size==0){
          this.$message.error('所选时间范围内暂无数据导出，请重新选择！')
        }
        handleExcelResponse(res);
        this.exportStationVisableDialog = false;
      } finally {
        loading.close();
      }
    },

    //按财务导出
    exportAffairsOpen(){
      this.exportAffairsVisableDialog = true;
    },
    async exportAffairsClick () {//按财务导出
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        const res = await exportAffairs(this.formCreate);
        if(res.data.size==0){
          this.$message.error('所选时间范围内暂无数据导出，请重新选择！')
        }
        handleExcelResponse(res);
        this.exportAffairsVisableDialog = false;
      } finally {
        loading.close();
      }
    },
    //导入
    handleImport(item) {
      let file = item.file;
      this.$message.info("正在导入，请耐心等待！");
      let param = new FormData();
      param.append("file", file);
      saveBatch(param).then(() => {
        this.$notify({
          title: "成功",
          message: "导入成功",
          type: "success",
          duration: 2000
        });
        this.getList(this.page);
      });
    },
    selectionChange(tableData){
      this.selectKeys=tableData;
      this.selectDateVals=[];
      this.selectLineVals=[];
      for(let i=0;i<tableData.length;i++){
        this.selectDateVals.push(tableData[i].tradeDate);
        this.selectLineVals.push(tableData[i].lineCode);
      }
    },
    confirmOpen(){
      this.confirmVisableDialog = true;
    },
    confirmClick(){//一键确认
      if(this.selectKeys.length<=0){
        this.$message.warning("请选择需要确认的数据！");
        return ;
      }
      if(this.selectKeys.length>0){
        if(this.isConfirmStatus()==false){
          this.$message.info("请选择未确认的数据！");
          return;
        }
        clickConfirm({tradeDateList:this.selectDateVals.join(","),lineCodeList:this.selectLineVals.join(",")})
          .then(data => {
            this.$message.success('批量确认成功！')
            this.getList(this.page)
          }).catch(() => {
          }); 
      }
    },
    isConfirmStatus(){ // 是否确认
      for(let i=0;i<this.selectKeys.length;i++){
        let row=this.selectKeys[i];
        if(row.confirmStatus==1){
          return false
        }
      }
      return true;
    },
    refreshChange () {
      this.getList(this.page)
    },
    handleRecord (row, index) {
      this.recordTable = row
    },
    handleView (row, index) {
      this.previewTable = row
    },
    searchReset () {
      this.page.currentPage = 1
      this.getList(this.page);
    },
  }
}
</script>
<style lang="scss">
.bottom-menu-adjust {
  margin-top: -30px;
  text-align: center;
}
</style>